class Adjoin {
    constructor(vertex) {
        this.vertex = vertex;
        this.quantity = vertex.length;
        this.init();
    }

    init() {
        this.adjList = Array.from({
            length: this.quantity
        }, x => Array.from({
            length: this.quantity
        }, y => 0));
    }

    container(vert, id) {
        let tem = -1
        vert.forEach((item, index) => {
            if (item.value_id === id.value_id) tem = index
        })

        return tem
    }

    getVertexRow(id) {
        const index = this.container(this.vertex, id)

        const col = [];
        this.vertex.forEach((item, pIndex) => {
            col.push(this.adjoinArray[index + this.quantity * pIndex]);
        });
        return col;
    }

    getAdjoinVertexs(id) {
        return this.getVertexRow(id).map((item, index) => (item ? this.vertex[index] : '')).filter(Boolean);
    }

    setAdjoinVertexs(id, sides) {
        const pIndex = this.container(this.vertex, id)
        sides.forEach((item) => {
            const index = this.container(this.vertex, item)
            console.log(pIndex, index, "12312312")
            // this.adjoinArray[pIndex * this.quantity + index] = 1;
            this.adjList[pIndex][index] = 1
        });
    }

    getRow(vertex) {
        const index = this.container(this.vertex, vertex)
        return this.adjList[index]
    }

    getRows(arr) {
        let rows = []
        arr.map(vertex => {
            rows.push(this.getRow(vertex))
        })
        return rows
    }

    // 并集
    getCollection(params) {

        const rows = this.getRows(params)
        console.log(rows, "rows")
        let a = rows.map((item, index) => item && this.vertex[index]).filter(Boolean)
        console.log(a, 123)
        return a

    }

    // 交集
    getUnions() {

    }
}

class ShopAdjoin extends Adjoin {
    constructor(commoditySpecs, data) {
        super(commoditySpecs.reduce((total, current) => [
            ...total,
            ...current.list,
        ], []));
        this.commoditySpecs = commoditySpecs;
        this.data = data;
        //将现有的规格顶点和边注册
        this.initOwnedVertex()
        // 未点击的时候 找到所有可达顶点
        this.findAllValidVertex()
        console.log(this.adjList, "矩阵列表")
    }

    initOwnedVertex() {
        this.data.forEach(items => {
            // console.log(this.data, "data")
            items.specs.forEach(vertex => {
                // 当前顶点边的添加
                this.setAdjoinVertexs(vertex, items.specs)
            })
        })
    }

    /**
     * 获取所有可选项
     * 未点击时 所有可达顶点均可点击
     * 所有顶点的交集
     */
    findAllValidVertex() {
        this.commoditySpecs.forEach((item) => {
            const params = [];
            item.list.forEach((value) => {
                if (this.vertex.indexOf(value) > -1) params.push(value);
            });
            // 同级点位创建
            params.forEach(item => {
                this.setAdjoinVertexs(item, params)
            })
        });
    }

    //获取所有可选项目
    querySpecsOptions(specsS) {
        let arr = specsS.filter(item => {
            return item
        })
        // 取到当前定点所有的边 交集
        // this.getCollection(arr)
    }


}

export {
    ShopAdjoin
}